package com.example.myapplication.service;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;

import com.example.myapplication.util.MusicUtil;

public class MusicService extends Service {

    private MusicUtil mu;
    /**
     * 音乐资源
     */
    private int id;

    public class MusicBinder extends Binder {

        public void start(int id){
            mu = new MusicUtil(MusicService.this, id);
            mu.start();
        }

        public void continues(){
            mu.start();
        }

        public void pause(){
            mu.pause();
        }

        public void stop(){
            mu.stop();
        }

        public void release(){
            mu.release();
        }

        public boolean isPlaying(){
            return mu.isPlaying();
        }

        public void setLooping(boolean bool){
            mu.setLooping(bool);
        }

    }

    MusicBinder binder = new MusicBinder();

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        id = intent.getIntExtra("name",0);
        mu = new MusicUtil(this, id);
        return binder;
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public boolean onUnbind(Intent intent) {
        mu.release();
        return super.onUnbind(intent);
    }

    @Override
    public void onDestroy() {
        Log.i("Music","service closed");
        super.onDestroy();
    }

}
